const path = require('path');
const gulp = require('gulp');
const gulpPostcss = require('gulp-postcss');
const gulpSass = require('gulp-sass');
const gulpBabel = require('gulp-babel');
const gulpConcat = require('gulp-concat');
const gulpUglify = require('gulp-uglify');
const gulpRename = require('gulp-rename');
const gulpCleanCSS = require('gulp-clean-css');
const gulpSourcemaps = require('gulp-sourcemaps');
const gulpFileInclude = require('gulp-file-include');
const autoprefixer = require('autoprefixer');
const del = require('del');
gulpSass.compiler = require('node-sass');

// 配置路径
const config = {
    // 公共目录
    public: {
        // 静态文件，不需要处理，直接移动
        static: {
            src: './public/static/**/*',
            dest: '../dest/public/static'
        },
        // 公共scss文件
        scss: {
            src: './public/scss/*.scss',
            dest: '../dest/public'
        },
        // 公共js文件
        js: {
            src: './public/js/*.js',
            dest: '../dest/public'
        }
    },
    // 前端目录
    index: {
        tpl: {
            src: './index/tpl/*.html'
        },
        html: {
            src: './index/*.html',
            dest: '../dest/index'
        },
        scss: {
            src: './index/scss/*.scss',
            dest: '../dest/index'
        },
        js: {
            src: './index/js/*.js',
            dest: '../dest/index/js'
        }
    },
    // 后台目录
    admin: {
        tpl: {
            src: './admin/tpl/*.html'
        },
        html: {
            src: './admin/*.html',
            dest: '../dest/admin'
        },
        scss: {
            src: './admin/scss/*.scss',
            dest: '../dest/admin'
        },
        js: {
            src: './admin/js/*.js',
            dest: '../dest/admin/js'
        }
    }
};

// 清除资源文件
function clean() {
    return del(['../dest/*'], {
        force: true
    });
}

// ===============================================================
// 公共静态文件
function staticPublic() {
    return gulp.src(config.public.static.src).pipe(gulp.dest(config.public.static.dest));
}

// 公共scss文件
function scssPublic() {
    return (
        gulp
            .src(config.public.scss.src)
            .pipe(gulpSourcemaps.init())
            .pipe(gulpSass())
            .pipe(gulpPostcss([autoprefixer({})]))
            // .pipe(gulpCleanCSS({ fotmat: 'beautify' }))
            .pipe(gulpSourcemaps.write())
            .pipe(gulpConcat('public.min.css'))
            .pipe(gulp.dest(config.public.scss.dest))
    );
}

// 公共js文件
function jsPublic() {
    return gulp
        .src(config.public.js.src)
        .pipe(gulpSourcemaps.init())
        .pipe(gulpBabel())
        .pipe(gulpConcat('public.min.js'))
        .pipe(gulpSourcemaps.write())
        .pipe(gulp.dest(config.public.js.dest));
}

// ===============================================================
// 前端html文件
function htmlIndex() {
    return gulp
        .src(config.index.html.src)
        .pipe(gulpFileInclude())
        .pipe(gulp.dest(config.index.html.dest));
}

// 前端scss文件
function scssIndex() {
    return (
        gulp
            .src(config.index.scss.src)
            .pipe(gulpSourcemaps.init())
            .pipe(gulpSass())
            .pipe(gulpPostcss([autoprefixer({})]))
            // .pipe(gulpCleanCSS({ fotmat: 'beautify' }))
            .pipe(gulpSourcemaps.write())
            .pipe(gulpConcat('index.min.css'))
            .pipe(gulp.dest(config.index.scss.dest))
    );
}

// 前端js文件
function jsIndex() {
    return (
        gulp
            .src(config.index.js.src)
            .pipe(gulpSourcemaps.init())
            .pipe(gulpBabel())
            .pipe(gulpSourcemaps.write())
            // .pipe(gulpConcat('index.min.js'))
            .pipe(gulp.dest(config.index.js.dest))
    );
}

// ===============================================================
// 后台html文件
function htmlAdmin() {
    return gulp
        .src(config.admin.html.src)
        .pipe(gulpFileInclude())
        .pipe(gulp.dest(config.admin.html.dest));
}

// 后台scss文件
function scssAdmin() {
    return (
        gulp
            .src(config.admin.scss.src)
            .pipe(gulpSourcemaps.init())
            .pipe(gulpSass())
            .pipe(gulpPostcss([autoprefixer({})]))
            // .pipe(gulpCleanCSS({ fotmat: 'beautify' }))
            .pipe(gulpSourcemaps.write())
            .pipe(gulpConcat('admin.min.css'))
            .pipe(gulp.dest(config.admin.scss.dest))
    );
}

// 后台js文件
function jsAdmin() {
    return (
        gulp
            .src(config.admin.js.src)
            .pipe(gulpSourcemaps.init())
            .pipe(gulpBabel())
            .pipe(gulpSourcemaps.write())
            // .pipe(gulpConcat('admin.min.js'))
            .pipe(gulp.dest(config.admin.js.dest))
    );
}

// ===============================================================
gulp.watch(config.public.static.src, staticPublic);
gulp.watch(config.public.scss.src, scssPublic);
gulp.watch(config.public.js.src, jsPublic);
gulp.watch(config.index.tpl.src, htmlIndex);
gulp.watch(config.index.html.src, htmlIndex);
gulp.watch(config.index.scss.src, scssIndex);
gulp.watch(config.index.js.src, jsIndex);
// gulp.watch(config.admin.tpl.src, htmlAdmin);
// gulp.watch(config.admin.html.src, htmlAdmin);
// gulp.watch(config.admin.scss.src, scssAdmin);
// gulp.watch(config.admin.js.src, jsAdmin);
exports.default = gulp.series(
    clean,
    gulp.parallel(
        //
        staticPublic,
        scssPublic,
        jsPublic,
        scssIndex,
        jsIndex,
        htmlIndex
        // scssAdmin,
        // jsAdmin,
        // htmlAdmin
    )
);
